/*
 * Wazuh Vulnerability scanner - Scan Orchestrator
 * Copyright (C) 2015, Wazuh Inc.
 * March 4, 2024.
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public
 * License (version 2) as published by the FSF - Free Software
 * Foundation.
 */

#ifndef _ALERT_CLEAR_BUILDER_HPP
#define _ALERT_CLEAR_BUILDER_HPP

#include "chainOfResponsability.hpp"
#include "databaseFeedManager.hpp"
#include "numericHelper.h"
#include "scanContext.hpp"
#include "timeHelper.h"

/**
 * @brief TAlertClearBuilder class.
 * This class is responsible for building the alert details for the clear alert.
 * It receives the scan context and the database feed manager and returns the scan context with the alert details.
 * Its neccessary to have the context->m_isInventoryEmpty populated with the inventory status.
 * If the inventory is empty, the alert will not be generated, this is because syscollector send and integrity clear
 * event each scan if the inventory is empty or if the inventory is disabled.
 *
 * @tparam TDatabaseFeedManager database feed manager type.
 * @tparam TScanContext scan context type.
 */
template<typename TDatabaseFeedManager = DatabaseFeedManager, typename TScanContext = ScanContext>
class TAlertClearBuilder final : public AbstractHandler<std::shared_ptr<TScanContext>>
{
private:
    std::shared_ptr<TDatabaseFeedManager> m_databaseFeedManager;

public:
    // LCOV_EXCL_START
    /**
     * @brief AlertClearBuilder constructor.
     *
     * @param databaseFeedManager Database feed manager.
     */
    explicit TAlertClearBuilder(std::shared_ptr<TDatabaseFeedManager>& databaseFeedManager)
        : m_databaseFeedManager(databaseFeedManager)
    {
    }

    /**
     * @brief Class destructor.
     *
     */
    ~TAlertClearBuilder() = default;
    // LCOV_EXCL_STOP

    /**
     * @brief Handles request and passes control to the next step of the chain.
     *
     * @param data Scan context.
     * @return std::shared_ptr<ScanContext> Abstract handler.
     */
    std::shared_ptr<TScanContext> handleRequest(std::shared_ptr<TScanContext> data) override
    {
        // If the database is empty, we don't need to send the alert.
        if (!data->m_isInventoryEmpty)
        {
            nlohmann::json report;

            report["vulnerability"]["status"] = "Clear";
            report["vulnerability"]["title"] =
                "There is no information of installed packages. Vulnerabilities cleared.";
            report["vulnerability"]["type"] = "Packages";

            data->m_alerts["clear"] = std::move(report);
        }

        return AbstractHandler<std::shared_ptr<TScanContext>>::handleRequest(std::move(data));
    }
};

using AlertClearBuilder = TAlertClearBuilder<>;

#endif // _ALERT_CLEAR_BUILDER_HPP
